package org.jeecg.gen.sqlTojava;

import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.codegenerate.generate.impl.CodeGenerateOne;
import org.jeecgframework.codegenerate.generate.impl.CodeGenerateOneToMany;
import org.jeecgframework.codegenerate.generate.pojo.TableVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.MainTableVo;
import org.jeecgframework.codegenerate.generate.pojo.onetomany.SubTableVo;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器入口【一对多】
 *
 * @Author 张代浩
 * @site www.jeecg.org
 */
@Slf4j
public class GeneratorTableToCode {

    /**
     * 数据模型，生成方法
     *
     * @param args
     */

    public static void main(String[] args) {
        String entityPackage_Agent = "agent";
        String entityPackage_Common = "common";
        String entityPackage_company = "company";
        String entityPackage_customer = "customer";
        String entityPackage_platform = "platform";
        String entityPackage_portal = "portal";


        ////////////////////*******************XIAOBAO************//////////////////////////
        ////////////////////*******************agent************//////////////////////////
        List<String[]> agent = new ArrayList<>();
        agent.add(new String[]{"xb_agent", "Agent", entityPackage_Agent, "代理商"});
        agent.add(new String[]{"xb_agent_purse", "AgentPurse", entityPackage_Agent, "代理钱包"});
        agent.add(new String[]{"xb_agent_purse_record", "AgentPurseRecord", entityPackage_Agent, "代理钱包记录"});
        agent.add(new String[]{"xb_agent_token_money_order", "AgentTokeMoneyOrder", entityPackage_Agent, "代理提现申请"});
        doAll(agent);
        ////////////////////*******************common************//////////////////////////
        List<String[]> common = new ArrayList<>();
        common.add(new String[]{"xb_address", "Address", entityPackage_Common, "地址"});
        common.add(new String[]{"xb_bark_card", "BarkCard", entityPackage_Common, "银行卡"});
        common.add(new String[]{"xb_feedback", "Feedback", entityPackage_Common, "反馈"});
        common.add(new String[]{"xb_Region", "Region", entityPackage_Common, "省市区"});
        common.add(new String[]{"xb_qsc_life", "QscLife", entityPackage_Common, "质维码生命周期"});
        doAll(common);

        ////////////////////*******************company************//////////////////////////
        List<String[]> company = new ArrayList<>();
        company.add(new String[]{"xb_company", "Company", entityPackage_company, "公司"});
        company.add(new String[]{"xb_company_edit_apply", "CompanyEditApply", entityPackage_company, "公司修改申请"});
        company.add(new String[]{"xb_company_purse", "CompanyPurse", entityPackage_company, "公司钱包（质维码额度）"});
        company.add(new String[]{"xb_cooperate_company", "CooperateCompany", entityPackage_company, "合作公司包含客户和供货商"});

        //--产品--//
        company.add(new String[]{"xb_product", "Product", entityPackage_company, "产品"});
        company.add(new String[]{"xb_product_spec", "ProductSpec", entityPackage_company, "产品配置"});
        company.add(new String[]{"xb_product_spec_item", "ProductSpecItem", entityPackage_company, "产品配置明细"});
        company.add(new String[]{"xb_product_spec_combination", "ProductSpecCombination", entityPackage_company, "产品配置组合"});
        company.add(new String[]{"xb_product_spec_combination_certify", "ProductSpecCombinationCertify", entityPackage_company, "产品配置组合认证信息"});
        //---订单---//
        company.add(new String[]{"xb_taocan_order", "TaocanOrder", entityPackage_company, "套餐订单"});
        company.add(new String[]{"xb_Qsc_quota_order", "QscQuotaOrder", entityPackage_company, "质维额度顶订单"});
        company.add(new String[]{"xb_tax_receipt_order", "TaxReceiptOrder", entityPackage_company, "税票申请"});
        //--质维码相关--//
        company.add(new String[]{"xb_export_order", "ExportOrder", entityPackage_company, "出货单"});
        company.add(new String[]{"xb_export_order_item", "ExportOrderItem", entityPackage_company, "出货单明细"});
        company.add(new String[]{"xb_import_order", "ImportOrder", entityPackage_company, "出货单"});
        company.add(new String[]{"xb_import_order_item", "ImportOrderItem", entityPackage_company, "出货单明细"});


        company.add(new String[]{"xb_qsc_apply", "QscApply", entityPackage_company, "质维码申请"});
        company.add(new String[]{"xb_Qsc_binding", "QscBinding", entityPackage_company, "质维码大小码绑定"});
        company.add(new String[]{"xb_Qsc_change_record", "QscChangeRecord", entityPackage_company, "质维码变更记录"});
        company.add(new String[]{"xb_Qsc_company_relation", "QscCompanyRelation", entityPackage_company, "公司质购买的维码"});
        company.add(new String[]{"xb_Qsc_quota_record", "QscQuotaRecord", entityPackage_company, "质维额度变化记录"});
        company.add(new String[]{"xb_storehouse", "Storehouse", entityPackage_company, "仓库"});
        company.add(new String[]{"xb_storehouse_store_combination", "StorehouseStoreCombination", entityPackage_company, "仓库产品配置库存"});
        doAll(company);
        ////////////////////*******************customer************//////////////////////////
        List<String[]> customer = new ArrayList<>();
        customer.add(new String[]{"xb_customer", "Customer", entityPackage_customer, "用户"});
        doAll(customer);
        ////////////////////*******************platform************//////////////////////////
        List<String[]> paltform = new ArrayList<>();
        paltform.add(new String[]{"xb_brand", "Brand", entityPackage_platform, "品牌"});
        paltform.add(new String[]{"xb_quantity_safeguard_code", "QuantitySafeguardCode", entityPackage_platform, "质维码"});
        paltform.add(new String[]{"xb_qsc_record", "QscRecord", entityPackage_platform, "码质维码生产分配记录"});
        paltform.add(new String[]{"xb_product_category", "ProductCategory", entityPackage_platform, "产品分类三级"});
        paltform.add(new String[]{"xb_industry", "Industry", entityPackage_platform, "行业"});
        paltform.add(new String[]{"xb_system_param", "SystemParam", entityPackage_platform, "系统参数"});
        paltform.add(new String[]{"xb_taocan", "Taocan", entityPackage_platform, "套餐概述"});
        paltform.add(new String[]{"xb_taocan_item", "TaocanItem", entityPackage_platform, "套餐明细"});
        doAll(paltform);
        ////////////////////*******************guanwang************//////////////////////////
        List<String[]> portal = new ArrayList<>();
        portal.add(new String[]{"xb_news", "News", entityPackage_portal, "新闻"});
        doAll(portal);

        System.out.println(agent.size()+common.size()+company.size()+paltform.size()+portal.size()+customer.size());
        /**
         * 在其他表完成后运行
         */
//        无效
//        paltform.add(new String[]{"xb_qsc_relation", "QscRelation", entityPackage_Platform, "质维码关系"});
    }

    private static void doAll(List<String[]> list) {
//
        list.forEach(table -> {
            TableVo tableVo = getTable(table[0], table[1], table[2], table[3]);
            // 数据模型,代码生成
            generator(tableVo);
        });

    }


    /*********************************************************** account *****************************************/
    private static void doAccountOneToMore(String entityPackage) {
        log.info("关于account主从创建");
        // 第一步：设置主表配置
        MainTableVo accountMainTable = new MainTableVo();
        accountMainTable.setTableName("ACCOUNT");// 表名
        accountMainTable.setEntityName("Account"); // 实体名
        accountMainTable.setEntityPackage(entityPackage); // 包名
        accountMainTable.setFtlDescription("账号"); // 描述
        /**
         * 无效jeecg只支持uuid
         */
        // accountMainTable.setPrimaryKeyPolicy("AUTO");

        // 第二步：设置子表集合配置
        List<SubTableVo> subTables = new ArrayList<SubTableVo>();
        // [1].子表一用户
        addUserTable(subTables, entityPackage);
        // [2].子表二公司
        addCompanyTable(subTables, entityPackage);
        // [3].子表三文章
        addArticleTable(subTables, entityPackage);
        // [4].子表名片
        addCardTable(subTables, entityPackage);
        // [5].子表反馈
        addFeedbackTable(subTables, entityPackage);
        // [6].子表加盟
        addJoinTable(subTables, entityPackage);
        // [7].字表消息
        addNoticeTable(subTables, entityPackage);
        // [8].字表记录
        addRecordTable(subTables, entityPackage);
        // [8].字表关系
        addRelationTable(subTables, entityPackage);

        accountMainTable.setSubTables(subTables);

        // 第三步：一对多(父子表)数据模型,代码生成
        generator(accountMainTable, subTables);
    }

    private static void addRelationTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub

        SubTableVo table = getTable("relation", "Relation", entityPackage, "关系（关注、收藏、点赞）", "account_id");
        subTables.add(table);

    }

    private static void addRecordTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub

        SubTableVo table = getTable("record", "Record", entityPackage, "记录", "doId");
        subTables.add(table);
    }

    private static void addNoticeTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub
        SubTableVo table = getTable("notice", "Notice", entityPackage, "消息", "beId");
        subTables.add(table);
    }

    private static void addJoinTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub

        SubTableVo table = getTable("join", "Join", entityPackage, "加盟信息", "account_id");
        subTables.add(table);

    }

    private static void addFeedbackTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub
        SubTableVo table = getTable("feedback", "Feedback", entityPackage, "用户反馈", "account_id");
        subTables.add(table);

    }

    private static void addCardTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub
        SubTableVo table = getTable("business_card", "BusinessCard", entityPackage, "名片", "account_id");
        subTables.add(table);

    }

    private static void addArticleTable(List<SubTableVo> subTables, String entityPackage) {
        // TO Auto-generated method stub
        SubTableVo articleTable = getTable("article", "Article", entityPackage, "文章", "account_id");
        subTables.add(articleTable);
    }

    /**
     * @param subTables
     * @Title: addUserTable
     * @Description: 用户信息表&作者信息表
     * @return: void
     */
    private static void addUserTable(List<SubTableVo> subTables, String entityPackage) {
        SubTableVo userTable = getTable("user_info", "User", entityPackage, "用户信息", "account_id");
        subTables.add(userTable);

    }

    /**
     * @param subTables
     * @Title: addCompanyTable
     * @Description: 公司信息表
     * @return: void
     */
    private static void addCompanyTable(List<SubTableVo> subTables, String entityPackage) {
        SubTableVo companyTable = getTable("company_info", "CompanyInfo", entityPackage, "公司信息", "account_id");
        subTables.add(companyTable);
    }

    /**
     * @param tableName      表名
     * @param entityName     实体名
     * @param entityPackage1 包名
     * @param tableDesc      描述
     * @return
     * @Title: getTable
     * @Description: 构建实体与表关系
     * @return: TableVo
     */
    private static TableVo getTable(String tableName, String entityName, String entityPackage1, String tableDesc) {

        TableVo tableVo = new TableVo();
        tableVo.setTableName(tableName);
        tableVo.setEntityName(entityName);
        tableVo.setEntityPackage(entityPackage1);
        tableVo.setFtlDescription(tableDesc);
        return tableVo;
    }

    /**
     * @param tableName      // 表名
     * @param entityName     // 实体名
     * @param entityPackage1 // 包名
     * @param tableDesc      // 描述
     * @param foreignKey     // 子表外键参数配置
     * @return
     * @Title: getTable
     * @Description: 构建实体与表关系
     * @return: SubTableVo
     */
    private static SubTableVo getTable(String tableName, String entityName, String entityPackage1, String tableDesc,
                                       String foreignKey) {
        SubTableVo companyTable = new SubTableVo();
        companyTable.setTableName(tableName);
        companyTable.setEntityName(entityName);
        companyTable.setEntityPackage(entityPackage1);
        companyTable.setFtlDescription(tableDesc);

        /*
         * 说明: a) 子表引用主表主键ID作为外键，外键字段必须以_ID结尾; b) 主表和子表的外键字段名字，必须相同（除主键ID外）; c)
         * 多个外键字段，采用逗号分隔;
         */
        companyTable.setForeignKeys(new String[]{foreignKey});
        return companyTable;
    }

    /**
     * @param tableVo
     * @Title: generator
     * @Description: 生成单表代码
     * @return: void
     */
    private static void generator(TableVo tableVo) {
        log.info(
                "根据" + tableVo.getFtlDescription() + "(" + tableVo.getTableName() + "表)创建实体" + tableVo.getEntityName());
        try {
            new CodeGenerateOne(tableVo).generateCodeFile(null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param mainTable
     * @param subTables
     * @Title: generator
     * @Description: 生成主从代码
     * @return: void
     */
    private static void generator(MainTableVo mainTable, List<SubTableVo> subTables) {
        // TO Auto-generated method stub

        try {
            new CodeGenerateOneToMany(mainTable, subTables).generateCodeFile(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
